package com.aaa.service.impl;

import com.aaa.dao.S_FrontTestDao;
import com.aaa.service.S_FrontTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class S_FrontTestServiceImpl implements S_FrontTestService {
    @Autowired
    private S_FrontTestDao dao;

    @Override
    public List<Map> selQuestions(Integer exam_id) {

        return dao.selQuestions(exam_id);
    }

    @Override
    public String selectExam(Integer exam_id) {
        return dao.selectExam(exam_id);
    }

    @Override
    public int getAnswer(Map map) {
        String answer=dao.getAnswer(map);
        if (map.get("answer").equals(answer)){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
        public void addScorce(int exam_id, int score) {
        //获取考试人数
        Integer exam_people=dao.getPeople(exam_id);
        Map map=new HashMap();
        map.put("exam_people",exam_people);
        map.put("exam_id",exam_id);
        //看看数据库是否有这个公司
        List<Map> list=dao.getSco(exam_id);
        if (list.size()>0){
            //以前的分数
            Integer a= (Integer) list.get(0).get("total_score");
            //加起来
            score+=a;
            map.put("total_score",score);
            //平均分
            map.put("average1",score/exam_people);
            dao.updScorce(map);

        }else {
            //总分
            map.put("total_score",score);
            //平均分
            map.put("average1",score/exam_people);
            dao.addScorce(map);
        }
    }

    @Override
    public void updPeople(int id) {
        dao.updPeople(id);
    }
}
